<?php 
require ("pgfunc.php");
function is_url($str){
  return preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/", $str);
 }

$getoauthurl = "http://qqfarn.appspot.com/twiauth?";

if($_SERVER['REQUEST_METHOD'] == "GET")
{
	$callback = "http://".$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
	if (array_key_exists('oauth_token',$_GET) && array_key_exists('oauth_verifier',$_GET))
	{
		$gasaccesstokenurl = $getoauthurl.$_SERVER['QUERY_STRING']."&consumer_key=".$_COOKIE['consumer_key']."&consumer_secret=".$_COOKIE['consumer_secret'];
		$data = vget($gasaccesstokenurl);
		echo($data."<br />");
		$apiurl = substr($callback, 0 ,strpos($callback, 'twauth.php'));
		echo("Your Api Address is:<br />".$apiurl."twifw.php<br />");
		return;
	}
	if (array_key_exists('consumer_key',$_GET) && array_key_exists('consumer_secret',$_GET))
	{
		$consumer_key = $_GET['consumer_key'];
		$consumer_secret = $_GET['consumer_secret'];
		$consumer_key = trim($consumer_key);
		$consumer_secret = trim($consumer_secret);
		setcookie("consumer_key", $consumer_key, time()+200);
		setcookie("consumer_secret", $consumer_secret, time()+200);
	}
	$getoauthurl = $getoauthurl."consumer_key=".$consumer_key."&consumer_secret=".$consumer_secret."&callback=".$callback;
	$data = vget($getoauthurl);
	$dataurl = trim($data);
	if (is_url($dataurl))
		header("Location: $dataurl");
	else
		echo("The API you provide is illegal!<br />");
		echo($data);
}
?>